import api from '../../../api/index';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    dataList: [],
    dataListdesc: [],
    shape: null,
    iconlist: [
      { name: 'BMI', toFixedNum: 1, bg:['#ffcc00','#00cc00','#ff9900','#ff3300'], unit:'', code: 'XYZBXY001001', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/1.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/1-1.png'},
      { name: '体脂率', toFixedNum: 1, bg:['#00cc00','#00cc00','#ff9900','#ff3300'], unit:'%', code: 'lk1590141731679', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/2.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/2-1.png' },
      { name: '肌肉量', toFixedNum: 1, bg:['#ffcc00','#00cc00','#00cc00'], unit:'kg', code: 'HXJK00000009', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/3.png', imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/3-1.png'},
      // { name: '去脂体重', toFixedNum: 1, bg:['#f62c12','#00cc00','#f62c12'], unit:'kg', code: 'HXJK00000006', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon_pxzf.png' },
      { name: '基础代谢', toFixedNum: 1, bg:['#ff9900','#00cc00'],unit:'kcal', code: 'lk1590141731918', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/4.png', imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/4-1.png' },
      { name: '身体年龄', toFixedNum: 0, bg:['#00cc00','#00cc00','#ff9900'], unit:'岁', code: 'HXJK00000003', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/5.png', imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/5-1.png' },
      { name: '内脏脂肪等级', toFixedNum: 0, bg:['#00cc00','#ff9900','#ff6600'], unit:'',code: 'XYZBXY001035', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/6.png', imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/6-1.png' },
      { name: '肌肉率', toFixedNum: 1, bg:['#ffcc00','#00cc00','#ff9900'], unit:'%', code: 'XYZBXY001032', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/7.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/7-1.png' },
      { name: '水分率', toFixedNum: 1, bg:['#ffcc00','#00cc00','#00cc00'], unit:'%', code: 'XYZBXY001029', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/8.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/8-1.png' },
      { name: '蛋白率', toFixedNum: 1, bg:['#ffcc00','#00cc00','#ff9900'], unit:'%', code: 'lk1636612991835', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/9.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/9-1.png' },
      { name: '骨骼肌', toFixedNum: 1, bg:['#ffcc00','#00cc00','#ff9900'], unit:'kg', code: 'HXJK00000002', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/10.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/10-1.png' },
      { name: '肌肉控制', toFixedNum: 1, bg:['#f62c12','#3fc500','#f62c12'], unit:'kg', code: 'HXJK00000012', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/11.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/11-1.png' },
      // { name: '脂肪控制', toFixedNum: 1, bg:['#f62c12','#3fc500','#f62c12'], unit:'kg', code: 'HXJK00000013', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/12.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/12-1.png' },
      { name: '脂肪量', toFixedNum: 1, bg:['#ffcc00','#00cc00','#ff9900','#ff3300'  ], unit:'kg', code: 'HXJK00000008', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/13.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/13-1.png' },
      { name: '肥胖等级', bg:['#ffcc00','#00cc00','#ff9900','#ff6600','#ff3300','#ff0033','#ff0033'],code: 'HXJK00000007', img: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/14.png',imgs: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/icon/14-1.png' }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { examid } = options
    this.getLoadDesc(examid);
  },
  handlerTab() {
    this.setData({
      show: !this.data.show,
      staticImage:
      {
        iconOpen: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/xia.png',
        iconFold: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/shang.png'
      }
    });
  },
  getLoadDesc(examid) {
    api
      .post(`/api/exams/record/${examid}`, {
        data: {
        }
      })
      .then((res) => {
        // console.log(res);
        if (res[0].devices[0].items.length > 0) {
          let items = res[0].devices[0].items
          const updatedItems = items.map(item => {
            return {
              ...item,
              appendix: JSON.parse(item.appendix)
            };
          });
          this.setData({
            time: res[0].time,
            weightlist: updatedItems[0]
          });
          this.getdataList(updatedItems)
        }
      });
  },
  sortByCode(dataArray, orderArray) {
    // 创建一个对象映射，将code与对应的数据对象建立关系
    const codeMap = {};
    dataArray.forEach(data => {
      codeMap[data.code] = data;
    });

    // 根据给定数组的顺序对数据进行排序
    const sortedArray = orderArray.map(code => codeMap[code]);
    console.log(orderArray);
    if(sortedArray[0] !== undefined) {
      return sortedArray;
    } else {
      return null
    }

  },

  getdataList(items) {
    console.log('22222----------');
    let iconlist = this.data.iconlist
    // 排序使用下面的
    const sortKeyList = [
      'XYZBXY001007', 'XYZBXY001001', 'lk1590141731679', 'HXJK00000009', 'HXJK00000006', 'lk1590141731918', 'HXJK00000003',
      'XYZBXY001035', 'XYZBXY001032', 'XYZBXY001029', 'lk1636612991835','HXJK00000011','HXJK00000002','HXJK00000013','HXJK00000008','HXJK00000007',
    ]
    let shape = ['HXJK00000014']
    let weightago = ['HXJK00000015']
    let weightagoshow = null

    weightago = this.sortByCode(items, weightago)?this.sortByCode(items, weightago)[0].value: null
    console.log(weightago);
    shape = this.sortByCode(items, shape)?this.sortByCode(items, shape)[0].value: null
    weightagoshow = Number(weightago);
    weightago = Math.abs(weightagoshow);
    console.log(weightagoshow);
    weightagoshow = weightagoshow > 0 ? true: false
    console.log(weightagoshow);
    if (weightagoshow == 0) {
      weightagoshow = null
    }
    console.log(shape);
    const sortItemsList = this.sortByCode(items, sortKeyList)
    // const sortItemsList = items
    if(!sortItemsList) {
      return
    }
    const list = []
    // 过滤并添加items中的属性到iconlist对应的项目中
    sortItemsList.forEach(item => {
      if(item && item.code){
        const matchingItem = iconlist.find(iconItem => item.code === iconItem.code)
        if (matchingItem) {
          const obj = item
          if(matchingItem.toFixedNum) {
            obj.value = parseFloat(item.value).toFixed(matchingItem.toFixedNum)
          }
          obj.level = item.appendix.level
          obj.ticks = item.appendix.ticks
          obj.levels = item.appendix.levels
          obj.description = item.appendix.description
          obj.advice = item.appendix.advice
          obj.img = matchingItem.img
          obj.imgs = matchingItem.imgs
          obj.bg = matchingItem.bg
          obj.name = matchingItem.name
          obj.unit = matchingItem.unit
          list.push(obj)
        }
      }
    })
    console.log(list);
    for (let j = 0; j < list.length; j++) {
      console.log(list[j].levels);
      if(list[j].levels) {
        let array = list[j].levels
        console.log(array);
				for (let y = 0; y < array.length; y++) {
          console.log(list[j].level);
          console.log(array[y]);
          console.log(y);
					if(list[j].level == array[y])
					list[j].ids = y
				}
			}
    }
    console.log(list);
    // 输出处理后的updatedIconlist数组
    this.setData({
      dataList: list,
      shape,
      weightago,
      weightagoshow
    });
    console.log(list);
    var dataListdesc = list.filter(function(obj) {
      return obj.code !== 'HXJK00000012' && obj.code !== 'HXJK00000013';
    });
    this.setData({
      dataListdesc
    });
    console.log(this.data.dataListdesc,'dataListdescdataListdescdataListdesc')
  },
  toggleIconFold(e) {
    let index = e.currentTarget.dataset.index
    let value = `dataListdesc[${index}].iconFold`
    this.setData({
      [value]: !this.data.dataListdesc[index].iconFold
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})